package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.EmployeeBasicInfo;
import com.ruoyi.system.mapper.JobPositionMapper;
import com.ruoyi.system.domain.JobPosition;
import com.ruoyi.system.service.IJobPositionService;

/**
 * 工作岗位信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@Service
public class JobPositionServiceImpl implements IJobPositionService 
{
    @Autowired
    private JobPositionMapper jobPositionMapper;

    /**
     * 查询工作岗位信息
     * 
     * @param id 工作岗位信息主键
     * @return 工作岗位信息
     */
    @Override
    public JobPosition selectJobPositionById(String id)
    {
        return jobPositionMapper.selectJobPositionById(id);
    }

    /**
     * 查询工作岗位信息列表
     * 
     * @param jobPosition 工作岗位信息
     * @return 工作岗位信息
     */
    @Override
    public List<JobPosition> selectJobPositionList(JobPosition jobPosition)
    {
        return jobPositionMapper.selectJobPositionList(jobPosition);
    }

    /**
     * 新增工作岗位信息
     * 
     * @param jobPosition 工作岗位信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJobPosition(JobPosition jobPosition)
    {
        int rows = jobPositionMapper.insertJobPosition(jobPosition);
        insertEmployeeBasicInfo(jobPosition);
        return rows;
    }

    /**
     * 修改工作岗位信息
     * 
     * @param jobPosition 工作岗位信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJobPosition(JobPosition jobPosition)
    {
        jobPositionMapper.deleteEmployeeBasicInfoByEmployeeId(jobPosition.getId());
        insertEmployeeBasicInfo(jobPosition);
        return jobPositionMapper.updateJobPosition(jobPosition);
    }

    /**
     * 批量删除工作岗位信息
     * 
     * @param ids 需要删除的工作岗位信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJobPositionByIds(String[] ids)
    {
        jobPositionMapper.deleteEmployeeBasicInfoByEmployeeIds(ids);
        return jobPositionMapper.deleteJobPositionByIds(ids);
    }

    /**
     * 删除工作岗位信息信息
     * 
     * @param id 工作岗位信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJobPositionById(String id)
    {
        jobPositionMapper.deleteEmployeeBasicInfoByEmployeeId(id);
        return jobPositionMapper.deleteJobPositionById(id);
    }

    /**
     * 新增员工基本信息信息
     * 
     * @param jobPosition 工作岗位信息对象
     */
    public void insertEmployeeBasicInfo(JobPosition jobPosition)
    {
        List<EmployeeBasicInfo> employeeBasicInfoList = jobPosition.getEmployeeBasicInfoList();
        String id = jobPosition.getId();
        if (StringUtils.isNotNull(employeeBasicInfoList))
        {
            List<EmployeeBasicInfo> list = new ArrayList<EmployeeBasicInfo>();
            for (EmployeeBasicInfo employeeBasicInfo : employeeBasicInfoList)
            {
                employeeBasicInfo.setEmployeeId(id);
                list.add(employeeBasicInfo);
            }
            if (list.size() > 0)
            {
                jobPositionMapper.batchEmployeeBasicInfo(list);
            }
        }
    }
}
